<template>
  <div class="template-main">
    <router-view />
  </div>
</template>

<script lang="ts" setup>
import { onMounted, onUnmounted } from 'vue';
import { useStore } from 'vuex';
import { AvoidArea, AvoidAreaInfo, AvoidAreaType } from '@/types/windowCommon';

const store = useStore();

const handleAvoidAreaChange = (event: any): void => {
  const data = event.detail as AvoidAreaInfo;
  const areaType = data.type;
  if (areaType === AvoidAreaType.TYPE_SYSTEM) {
    store.commit('setTopAvoidArea', (window as any).webClass?.pxToVp(data.area.topRect.height));
  }
  if (areaType === AvoidAreaType.TYPE_NAVIGATION_INDICATOR) {
    store.commit('setBottomAvoidArea', (window as any).webClass?.pxToVp(data.area.bottomRect.height));
  }
};

onMounted(() => {
  if ((window as any).webClass) {
    const webClass = (window as any).webClass;
    const topArea = webClass.getAvoidArea(AvoidAreaType.TYPE_SYSTEM) as AvoidArea;
    const bottomArea = webClass.getAvoidArea(AvoidAreaType.TYPE_NAVIGATION_INDICATOR) as AvoidArea;
    store.commit('setTopAvoidArea', webClass.pxToVp(topArea.topRect.height));
    store.commit('setBottomAvoidArea', webClass.pxToVp(bottomArea.bottomRect.height));
  }
  window.addEventListener('avoidAreaChange', handleAvoidAreaChange);
});

onUnmounted(() => {
  window.removeEventListener('avoidAreaChange', handleAvoidAreaChange);
});
</script>

<style lang="less">
body {
  margin: 0;
  padding: 0;
}

#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

nav {
  padding: 30px;

  a {
    font-weight: bold;
    color: #2c3e50;

    &.router-link-exact-active {
      color: #42b983;
    }
  }
}

.template-main {
  height: 100vh;
  box-sizing: border-box;
}
</style>
